package com.tidc.seaquartz.service.impl;

import com.tidc.seaquartz.job.CheckCommitmentOutJob;
import com.tidc.seaquartz.job.TaskCountDownJob;
import com.tidc.seaquartz.service.MissionCountDownService;
import com.tidc.seaservermodel.entity.Commitment;
import com.tidc.seaservermodel.entity.Fres;
import com.tidc.seaservermodel.entity.UserTask;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author ：冯涛滔
 * @date ：Created in 2022/10/3 15:00
 * @description：
 * @modified By：
 * @version:
 */
@Service
@Slf4j
public class  MissionCountDownServiceImpl implements MissionCountDownService {
    @Resource
    private Scheduler scheduler;

    @Override
    public Fres<Integer> addTaskCountDown(UserTask userTask) {
        Fres<Integer> fres = null;
        JobDetail startJobDetail = JobBuilder.newJob(TaskCountDownJob.class)
                // 指定任务组名和任务名
                .withIdentity( userTask.getId()+"", "taskCountDownJob")
                // 添加一些参数，执行的时候用
                .usingJobData("id", userTask.getId())
                .usingJobData("taskId", userTask.getTaskId())
                .build();
        CronTrigger startCronTrigger = TriggerBuilder.newTrigger()
                // 指定触发器组名和触发器名
                .withIdentity(userTask.getId()+"","taskCountDownTrigger")
                .withSchedule(CronScheduleBuilder.cronSchedule(startTime(userTask.getLimitTime())))
                .build();
        // 将job和trigger添加到scheduler里
        try {
            scheduler.scheduleJob(startJobDetail, startCronTrigger);
            fres = Fres.Success();
        } catch (SchedulerException e) {
            log.error("任务倒计时设置失败 userTask {}",userTask);
            e.printStackTrace();
            fres = Fres.ErrorFre(1);
        }
        return fres;
    }

    @Override
    public Fres<Integer> checkCommitmentOut(Commitment commitment) {
        Fres<Integer> fres = null;
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("commitment",commitment);
        JobDetail startJobDetail = JobBuilder.newJob(CheckCommitmentOutJob.class)
                // 指定任务组名和任务名
                .withIdentity( commitment.getId()+"", "checkCommitmentOutJob")
                // 添加一些参数，执行的时候用
               .setJobData(jobDataMap)
                .build();

        CronTrigger startCronTrigger = TriggerBuilder.newTrigger()
                // 指定触发器组名和触发器名
                .withIdentity(commitment.getId()+"","checkCommitmentOutTrigger")
                .withSchedule(CronScheduleBuilder.cronSchedule(startTime(commitment.getTimeout())))
                .build();
        // 将job和trigger添加到scheduler里
        try {
            scheduler.scheduleJob(startJobDetail, startCronTrigger);
            fres = Fres.Success();
        } catch (SchedulerException e) {
            log.error("奖励兑换倒计时设置失败 Commitment {}",commitment);
            e.printStackTrace();
            fres = Fres.ErrorFre(1);
        }
        return fres;
    }

    /**
     * 获取一个启动时间字符串
     * @param hours 多少个启动
     *
     * @return
     */
    public static String startTime(Integer hours){
        LocalDateTime startTime = LocalDateTime.now();
//        设置过期检查时间
        startTime = startTime.plusHours(hours);
        return String.format("%d %d %d %d %d ? %d",
                startTime.getSecond(),
                startTime.getMinute(),
                startTime.getHour(),
                startTime.getDayOfMonth(),
                startTime.getMonth().getValue(),
                startTime.getYear());
    }
}
